@abstract
extends MoveComponentBase
class_name PlayerMoveComponentBase

@export var mover:CharacterBody2D
@export var max_speed:float = 128.0
@export var gravity:float = 12
@export_group("按键动作")
@export var left_action:String = "move_left"
@export var right_action:String = "move_right"
@export var up_action:String = "move_up"
@export var down_action:String = "move_down"

var input_dir:Vector2
var is_input_changed:bool = false
var external_velocity:Vector2 = Vector2.ZERO##外界设置的速度
var added_velocity:Vector2 = Vector2.ZERO##外界添加的速度

func set_velocity(velocity:Vector2):
	external_velocity = velocity

func set_added_velocity(velocity:Vector2):
	added_velocity = velocity

func get_input_dir()->Vector2:
	return input_dir

func get_velocity()->Vector2:
	return mover.velocity

func get_mover_pos()->Vector2:
	return mover.position

func _physics_process(delta: float) -> void:
	if !mover:
		return
	_update_input_dir()
	_move(delta)
	_check_external_setted_velocity()
	_check_added_velocity()
	mover.move_and_slide()

@abstract func _move(delta: float)

func _update_input_dir():
	var pre_input_dir = input_dir
	input_dir = Input.get_vector(left_action,right_action,up_action,down_action)
	if pre_input_dir !=  input_dir:
		is_input_changed = true
	else:
		is_input_changed = false

func _check_external_setted_velocity():
	if external_velocity != Vector2.ZERO:
		mover.velocity = external_velocity
		external_velocity = Vector2.ZERO

func _check_added_velocity():
	if added_velocity != Vector2.ZERO:
		mover.velocity += added_velocity
		added_velocity = Vector2.ZERO
